package com.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianfeng.util.ResultMsg;
import com.travel.entity.TbHome;
import com.travel.entity.TbHotel;
import com.travel.mapper.HomeMapper;
import com.travel.mapper.HotelMapper;
import com.travel.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HomeServiceImpl extends ServiceImpl<HomeMapper, TbHome> implements HomeService {

    @Autowired
    HotelMapper hotelMapper;

    @Override
    public ResultMsg getAllByHotelId(int hotelId) {
        QueryWrapper<TbHome> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id",hotelId);
        wrapper.eq("home_status",0);
        List<TbHome> homeList = super.list(wrapper);
        if (homeList!=null){
            return ResultMsg.success(homeList);
        }
        return ResultMsg.error(300,"没有房间");
    }

    @Override
    public ResultMsg getAllByHotelid(int hotelId, String homeType) {
        QueryWrapper<TbHome> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id",hotelId);
        wrapper.eq("home_status",0);
        if (!homeType.equals("all")){
            wrapper.eq("home_type",homeType);
        }else {
            // wrapper.like("home_type",homeType);
            wrapper.eq("home_type",homeType);
        }
        List<TbHome> homeList = super.list(wrapper);
        if (homeList!=null){
            return ResultMsg.success(homeList);
        }
        return ResultMsg.error(300,"没有房间");
    }

    @Override
    public ResultMsg getOneByHotelId(int hotelId) {
        TbHome home = super.getById(hotelId);
        if (home!=null){
            return ResultMsg.success(home);
        }
        return ResultMsg.error(300,"没有房间");
    }

    @Override
    public ResultMsg getAllByHotelIdPage(Integer hotelId, Integer current, Integer size) {
        QueryWrapper<TbHotel> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("hotel_id",hotelId);
        TbHotel tbHotel = hotelMapper.selectOne(wrapper1);

        Page<TbHome> homePage = new Page<>(current,size);
        QueryWrapper<TbHome> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id",hotelId);
        Page<TbHome> page = super.page(homePage, wrapper);
        List<TbHome> records = page.getRecords();
        for (TbHome record : records) {
            record.setHotelName(tbHotel.getHotelName());
        }
        if (page!=null || page.getSize()!=0){
            return ResultMsg.success(page);
        }
        return ResultMsg.error(300,"没有房间");
    }

    @Override
    public ResultMsg insertOne(TbHome home) {
        boolean insertHome = super.save(home);
        if (insertHome){
            return ResultMsg.success(200);
        }
        return ResultMsg.error(300,"插入失败");
    }

    @Override
    public ResultMsg updateOne(TbHome home) {
        boolean updateById = super.updateById(home);
        if (updateById){
            return ResultMsg.success(200);
        }
        return ResultMsg.error(300,"修改失败");
    }

    @Override
    public ResultMsg deleteOne(Integer homeId) {
        boolean remove = super.removeById(homeId);
        if (remove){
            return ResultMsg.success(200);
        }
        return ResultMsg.error(300,"删除失败");
    }

    @Override
    public ResultMsg getAllByHotelType(String homeType) {
        QueryWrapper<TbHome> wrapper = new QueryWrapper<>();
        if (!homeType.equals("all")){
            wrapper.eq("home_type",homeType);
        }
        List<TbHome> homeList = super.list(wrapper);
        if (homeList != null || homeList.size() != 0){
            return ResultMsg.success(homeList);
        }else {
            return ResultMsg.error(300,"出错，未找到酒店！");
        }
    }


    @Override
    public ResultMsg getByHomeid(int homeId) {
        TbHome home = super.getById(homeId);
        if (home!=null){
            return ResultMsg.success(home);
        }
        return ResultMsg.error(300,"没有房间");
    }
}
